package com.ruicar.afs.cloud.afscase.postloan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.common.utils.Const;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseContractInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseContractInfoService;
import com.ruicar.afs.cloud.afscase.postloan.service.CaseRepayPlanService;
import com.ruicar.afs.cloud.afscase.postloan.vo.CaseRepayPlanVO;
import com.ruicar.afs.cloud.afscase.postloan.config.ContractBasicSysConfig;
import com.ruicar.afs.cloud.basic.outer.api.common.dto.RepaymentPlanDTO;
import com.ruicar.afs.cloud.basic.outer.api.common.feign.CommonFeign;
import com.ruicar.afs.cloud.basic.outer.api.common.feign.CommonOuterQueryFeign;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.ContractMainInfoDto;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.*;

/**
 * @Description
 * @Author Peng Jinbo
 * @Date 2020/08/15 16:04
 */
@Service
@AllArgsConstructor
@Slf4j
public class CaseRepayPlanServiceImpl implements CaseRepayPlanService {

    /**
     * Common outer query feign
     */
    private CommonOuterQueryFeign commonOuterQueryFeign;
    /**
     * Contract basic sys config
     */
    private ContractBasicSysConfig contractBasicSysConfig;

    /**
     * Case contract info service
     */
    private CaseContractInfoService caseContractInfoService;


    private CommonFeign commonFeign;

    /**
     * Author Peng Jinbo
     * List repay plans list
     *
     * @param applyNo apply no
     * @return the list
     */
    @Override
    public List<CaseRepayPlanVO> listRepayPlans(String applyNo) {
        Assert.isTrue(StrUtil.isNotBlank(applyNo), "申请编号不可为空");

        Optional<CaseContractInfo> contractOptional = caseContractInfoService.list(
                Wrappers.<CaseContractInfo>lambdaQuery()
                        .eq(CaseContractInfo::getApplyNo, applyNo)
        ).stream().findFirst();

        ArrayList<CaseRepayPlanVO> retList = new ArrayList<>();
        if (contractOptional.isPresent()) {
            CaseContractInfo contractInfo = contractOptional.get();
            IResponse<List<RepaymentPlanDTO>> response = commonOuterQueryFeign.getRepaymentPlanInfoList(contractInfo.getContractNo(), contractSysHeaders());
            if (CommonConstants.SUCCESS.equals(response.getCode())) {
                List<RepaymentPlanDTO> repayDtoList = response.getData();
                if(CollectionUtil.isNotEmpty(repayDtoList)){
                    for (RepaymentPlanDTO repaymentPlanDTO : repayDtoList) {
                        CaseRepayPlanVO repayPlanVO = new CaseRepayPlanVO();
                        BeanUtil.copyProperties(repaymentPlanDTO,repayPlanVO);
                        if(Objects.nonNull(repayPlanVO.getStatus())) {
                            repayPlanVO.setStatusDesc(AfsEnumUtil.desc(repayPlanVO.getStatus()));
                        }
                        retList.add(repayPlanVO);
                    }
                }
            }
        }
        return retList;
    }

    @Override
    public List<ContractMainInfoDto> listContractInfo(String applyNo) {
        Assert.isTrue(StrUtil.isNotBlank(applyNo), "申请编号不可为空");
        List<CaseContractInfo> contractInfoList= caseContractInfoService.list(
                Wrappers.<CaseContractInfo>lambdaQuery()
                        .eq(CaseContractInfo::getApplyNo, applyNo)
        );
        List<ContractMainInfoDto> contractMainInfoDtoList = new ArrayList<>(contractInfoList.size());
        if(CollectionUtil.isNotEmpty(contractInfoList)){
            for (CaseContractInfo contractInfo : contractInfoList) {
                if(StrUtil.isNotBlank(contractInfo.getContractNo())) {
                    IResponse<ContractMainInfoDto> response = commonFeign.getContractMainInfo(contractInfo.getContractNo(),contractSysHeaders());
                    if(CommonConstants.SUCCESS.equals(response.getCode()) && Objects.nonNull(response.getData())){
                        contractMainInfoDtoList.add(response.getData());
                    }
                }
            }
        }
        return contractMainInfoDtoList;
    }


    /**
     * Author Peng Jinbo
     * Contract sys headers map
     *
     * @return the map
     */
    private Map contractSysHeaders(){
        Map<String,String> headers = new HashMap<>();
        headers.put(Const.HTTP_HEADER_CLIENT_ID, contractBasicSysConfig.getClientId());
        headers.put(Const.HTTP_HEADER_CLIENT_SECRET,contractBasicSysConfig.getClientSecret());
        return headers;
    }

}
